package com.future.service.impl;

import com.future.lock.Lock;
import com.future.mapper.OrderMapper;
import com.future.mapper.TradeMapper;
import com.future.util.RedisUtil;
import com.future.vo.Order;
import com.future.vo.Stock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Description:
 *
 * @author weiruibai.vendor
 * Date: 2022/9/20 10:15
 */
public class CommonService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommonService.class);
    //JUC提供锁
    protected ReentrantLock reentrantLock = new ReentrantLock();

    @Autowired
    protected JdbcTemplate jdbcTemplate;

    //自己实现的锁
    @Autowired
    protected Lock lock;

    @Autowired
    protected TradeMapper tradeMapper;

    @Autowired
    protected OrderMapper orderMapper;

    @Autowired
    protected RedisUtil redisUtil;

    public Stock select(int id) {
        return tradeMapper.select(id);
    }

    public void update(int id) {
        Stock stock = tradeMapper.select(id);
        tradeMapper.update(stock);
    }


    protected void doDBUpdate(Integer stock) {
        LOGGER.info("start insert and update to db");
        jdbcTemplate.update("update t_stock set stock=? where id=1", stock);
        Order order = buildOrder();
        orderMapper.insert(order);
        LOGGER.info("finish insert or update to db");
    }


    private Order buildOrder() {
        Order order = new Order();
        order.setCreateAt(new Date());
        order.setUserName(UUID.randomUUID().toString().substring(10));
        return order;
    }
}
